package com.mapbar.pay.service.impl;

import com.mapbar.pay.dao.UserDao;
import com.mapbar.pay.entity.User;
import com.mapbar.pay.service.UserService;
import com.mapbar.pay.util.CacheUtil;
import com.mapbar.pay.util.ProjectUtil;
import org.apache.log4j.Logger;

/**
 *@Date 创建时间 2010-03-04 09:14:54
 *@author fengbj
 * 
 */
public class UserServiceImpl implements UserService{

    public Logger logger = Logger.getLogger(this.getClass());//日志对象

    private UserDao userDao;

    /**
     * 用户登录
     * @param userName
     * @param pwd
     * @return 返回User对象，如果为空表示登录失败
     */
    @Override
    public User logon(String userName ,String pwd){
        if(userName == null || pwd == null){
            return null;
        }else{
            User user = userDao.logon(userName, pwd);
            if(user != null){
                logger.info("用户登录成功，user = [" + user.toString() +"] ，存入缓存");
                CacheUtil.set(ProjectUtil.USER_PREFIX_IN_CACHE + user.getUserId() , user);
            }
         return user;
        }

    }


    /**
     * 当用户信息过期时刷新用户信息
     * @param user
     * @return 更新后用户信息
     */
    @Override
    public void refreshUserInfo(User user){

        if(user == null){
            return;
        }else{
            userDao.refreshUserInfo(user);
            logger.info("用户登录成功，user = [" + user.toString() +"] ，存入缓存");
            CacheUtil.set(ProjectUtil.USER_PREFIX_IN_CACHE + user.getUserId() , user);
        }
        //return null;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User reg(String userName, String userPwd, String userEmail, String userPhone, String userSerial  , String type) {
       return userDao.reg(userName, userPwd, userEmail, userPhone, userSerial, type);
    }
}
